
package models.aaa;

import java.util.ArrayList;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import models.Store;

import org.apache.commons.lang3.StringUtils;

import play.db.jpa.JPA;

/**
 * 
 * @author laurent@opprecht.info
 */
@Entity
public class Identifier extends Node {

    public static class Finder {

        public TypedQuery<Identifier> createQuery(final String sql) {
            TypedQuery<Identifier> result = JPA.em().createQuery(sql, Identifier.class);
            return result;
        }
        
        public Query createUntypedQuery(final String sql) {
            Query result = JPA.em().createQuery(sql);
            return result;
        }
        
        public Query untypedQuery(final String sql, final Object... parameters) {
            Query result = createUntypedQuery(sql);
            for (int i = 0; i < parameters.length; i++) {
                result.setParameter(i + 1, parameters[i]);
            }
            return result;
        }

        public TypedQuery<Identifier> query(final String sql, final Object... parameters) {
            TypedQuery<Identifier> result = createQuery(sql);
            for (int i = 0; i < parameters.length; i++) {
                result.setParameter(i + 1, parameters[i]);
            }
            return result;
        }

        public TypedQuery<Identifier> query(final String sql) {
            return query(sql, new Object[] {});
        }

        public Identifier byValue(final String domain, final String value) {
            if (StringUtils.isEmpty(domain)) {
                return null;
            }
            if (StringUtils.isEmpty(value)) {
                return null;
            }

            String sql = "SELECT e FROM Identifier e WHERE e.domain = ? AND e.value = ?";
            //List<Identifier> items = query(sql, domain, value).getResultList();
            @SuppressWarnings("unchecked")
            ArrayList<Identifier> items = new ArrayList<Identifier>(untypedQuery(sql,domain,value).getResultList());
            for (Identifier item : items) {
                return item;
            }
            return null;
        }

    }

    public static final Finder find             = new Finder();

    @SuppressWarnings("unused")
    private static final long  serialVersionUID = -994557330115344519L;

    public static Identifier get(final String domain, final String value) {
        if (StringUtils.isEmpty(domain)) {
            return null;
        }
        if (StringUtils.isEmpty(value)) {
            return null;
        }

        Identifier result = find.byValue(domain, value);
        if (result != null) {
            return result;
        }

        result = new Identifier(domain, value);
        Store.save(result);
        return result;
    }

    protected String domain = null;
    protected String value  = null;

    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.MERGE }, fetch = FetchType.EAGER, optional = true)
    private Asset    asset  = null;

    public Identifier() {
        ;
    }

    public Identifier(final String domain, final String value) {
        setDomain(domain);
        setValue(value);
    }

    public String getDomain() {
        return domain;
    }

    protected void setDomain(final String value) {
        this.domain = value;
    }

    public String getValue() {
        return value;
    }

    protected void setValue(final String value) {
        this.value = value;
    }

    public Asset getAsset() {
        return asset;
    }

    public void setAsset(final Asset asset) {
        this.asset = asset;
    }
}
